package com.GreenIce.android.UI;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;

import com.GreenIce.android.R;
import com.GreenIce.android.datastructure.ConfigData;
import com.GreenIce.android.datastructure.ConfigData.WorldInfo;
import com.GreenIce.android.util.ResourceIdentifier;

public class LevelAdapter extends BaseAdapter{
	
	private Context context;
	private int levelId;
	private int worldId;
	private Integer[] level;
	public LevelAdapter(Context c, int l, int wid){
		context = c;
		levelId = l;
		worldId = wid;
		level = new Integer[l];
		for(int i=1; i<=l; i++){
			level[i-1] = ResourceIdentifier.getDrawableID("level"+i);
		}
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return levelId;
	}

	@Override
	public Object getItem(int pos) {
		// TODO Auto-generated method stub
		return pos;
	}

	@Override
	public long getItemId(int pos) {
		// TODO Auto-generated method stub
		return pos;
	}

	@Override
	public View getView(int pos, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		WorldInfo aWorld = ConfigData.getInstance().worldInfo.get(worldId);
		
		ImageView imgView = new ImageView(context);
		RatingBar rateStar = new RatingBar(context,null,android.R.attr.ratingBarStyleSmall);
		LinearLayout liner = new LinearLayout(context);
		
		rateStar.setNumStars(3);
		rateStar.setStepSize(1.0f);
		rateStar.setLayoutParams(new LayoutParams(
				LinearLayout.LayoutParams.WRAP_CONTENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));
		rateStar.setIsIndicator(true);
		if(aWorld.playNum >= pos){
//			Resources r = context.getResources();
//			Bitmap bmp = BitmapFactory.decodeResource(r, R.drawable.levelbg);
//			int bmpW = bmp.getWidth();
//			int bmpH = bmp.getHeight();
//			Bitmap nbmp = Bitmap.createBitmap(bmpW,bmpH,Config.ARGB_8888);
//			Canvas canvas = new Canvas(nbmp);
//			canvas.drawBitmap(bmp, 0, 0, null);
//			Paint paint = new Paint();	
//			paint.setColor(Color.RED);
//			paint.setTextSize(40);
//			paint.setTextAlign(Align.CENTER);
//			FontMetrics fontMetrics = paint.getFontMetrics(); 
//			float fontHeight = fontMetrics.bottom - fontMetrics.top;
//			float height = bmpH - (bmpH - fontHeight) / 2 - fontMetrics.bottom; 
//			canvas.drawText((arg0+1)+"", bmpW/2, height, paint);	
			imgView.setImageResource(level[pos]);	
			rateStar.setRating(aWorld.eachLevel[pos]);		
		}
		else{
			Resources r = context.getResources();
			Bitmap bmp = BitmapFactory.decodeResource(r, R.drawable.locklevel);
			imgView.setImageBitmap(bmp);
			rateStar.setVisibility(View.INVISIBLE);
		}
		liner.addView(imgView);
		liner.addView(rateStar);
		
		liner.setOrientation(LinearLayout.VERTICAL);
		liner.setGravity(Gravity.CENTER);
		return liner;
	}
}
